package com.huanying.goodjob.advertisment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;

@Service
public class AdvertismentServiceImpl implements AdvertismentService{

	@Autowired
	private BaseDao dao;
	@Override
	public void add_ad(Advertisment advertisment) throws Exception {
		// TODO Auto-generated method stub
		dao.save(advertisment);
	}

	@Override
	public void update_ad(Advertisment advertisment) throws Exception {
		// TODO Auto-generated method stub
		dao.merge(advertisment);
	}

	@Override
	public int delete_ad(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Advertisment set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		return dao.exculeByParams(hql, param);
	}

	@Override
	public PageBean search_ad(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Advertisment as c where 1=1");
		List<Object> param = new ArrayList<Object>();
		////////////////////
		if (map.get("province") != "" && map.get("province") != null) {
			hql.append(" and c.province.code like ?");
			param.add("%"+map.get("province")+"%");
		}
		if (map.get("city") != "" && map.get("city") != null) {
			hql.append(" and c.city.code like ?");
			param.add("%"+map.get("city")+"%");
		}

		if (map.get("county") != null && map.get("county") != "") {
			hql.append(" and c.county.code like ?");
			param.add("%"+map.get("county")+"%");
		}
		///////////////////////////
		
		
		
		if(map.get("title")!=null && map.get("title")!="")
		{
			hql.append(" and c.title like ?");
			param.add("%"+map.get("title")+"%");
		}
		if(map.get("content")!=null && map.get("content")!="")
		{
			hql.append(" and c.content like ?");
			param.add("%"+map.get("content")+"%");
		}
		if(map.get("status")!=null && map.get("status")!="")
		{
			hql.append(" and c.status=?");
			param.add(Integer.parseInt((String)map.get("status")));
		}
		
		
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public Advertisment get_ad(int id) throws Exception {
		// TODO Auto-generated method stub
		return (Advertisment)dao.load(Advertisment.class, id);
	}

	@Override
	public List<Advertisment> front_get_advertisment(Map<String,Object> map) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Advertisment as a where a.status=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("province_code")!=null && map.get("province_code")!="")
		{
			hql.append(" and a.province.code=?");
			param.add((String)map.get("province_code"));
		}
		if(map.get("city_code")!=null && map.get("city_code")!="")
		{
			hql.append(" and a.city.code=?");
			param.add((String)map.get("city_code"));
			}
		if(map.get("county_code")!=null && map.get("county_code")!="")
		{
			hql.append(" and a.county.code=?");
			param.add(map.get("county_code"));
		}
		return (List<Advertisment>)dao.exculeHqlByParams(hql.toString(), param);
	}

}
